/**
 * 游戏 容器 对象
 */
class AppContainer
    extends egret.DisplayObjectContainer {

    /**
     * 单例实例
     */
    private static instance: AppContainer = null;

    /**
     * 编译顺序 标识
     */
    public static referenceFlag: string = GameStartScene.referenceFlag;

    /**
     * 相关属性
     */
    private gameFireScene: GameFireScene = null; // 场景 实例
    private gameEndScene: GameEndScene = null;
    private gamePauseScene: GamePauseScene = null;
    private gameStartScene: GameStartScene = null;

    private tickLogTime: number = 0; // Ticker 实时 时间 记录
    private pauseActionTime: number = 0; // 限制 暂停 函数 执行 时间
    private currentApplyScene: string = ""; // 当前 应用 场景


    /**
     * 构造函数
     */
    public constructor() {
        super();
    }

    /**
     * 获取 单例实例
     */
    public static get getInstance(): AppContainer {

        if (AppContainer.instance == null) {
            AppContainer.instance = new AppContainer();
        }
        return AppContainer.instance;
    }

    /**
     * 初始化 函数
     */
    public init(): void {

        this.width = GameHelper.getInstance.stageWidth; // 1.0 初始化 宽高
        this.height = GameHelper.getInstance.stageHeight;

        this.setCurrentScene(GameStartScene.selfName); // 初始化 当前 应用 场景

        KeyBoardManager.getInstance.init(); // 2.0 初始化 键盘事件 管理器

        // 3.0 初始化 监听事件
        EventDispatcherManager.getInstance.addEventListener(AppEvent.EVENT_SHOWGAMEFIRESCENE_NTFY, this.showGameFireScene, this);
        EventDispatcherManager.getInstance.addEventListener(AppEvent.EVENT_SHOWGAMEENDSCENE_NTFY, this.showGameEndScene, this);
        EventDispatcherManager.getInstance.addEventListener(AppEvent.EVENT_SHOWGAMEPAUSESCENE_NTFY, this.showGamePauseScene, this);
        EventDispatcherManager.getInstance.addEventListener(AppEvent.EVENT_SHOWGAMESTARTSCENE_NTFY, this.showGameStartScene, this);
        EventDispatcherManager.getInstance.addEventListener(AppEvent.EVENT_ENTERGAMEFIRESCENE_NTFY, this.enterGameFireScene, this);
        EventDispatcherManager.getInstance.addEventListener(AppEvent.EVENT_OPENGAMEPAUSESCENE_NTFY, this.openGamePauseScene, this);
        EventDispatcherManager.getInstance.addEventListener(AppEvent.EVENT_RETURNGAMESTARTSCENE_NTFY, this.returnGameStartScene, this);
        EventDispatcherManager.getInstance.addEventListener(AppEvent.EVENT_RETURNGAMEFIRESCENE_NTFY, this.returnGameFireScene, this);

        this.initTickerRegister(); // 4.0 初始化 Ticker 心跳机制 注册函数
    }

    /**
     * 初始化 Ticker 心跳机制 注册函数
     */
    private initTickerRegister(): void {
        egret.Ticker.getInstance().register(this.onTick, this); // 注册 事件 侦听
    }

    /**
     * 心跳 函数
     * @param dt
     */
    private onTick(dt: number): void {

        let tickLogTime: number = GameHelper.getInstance.tickLogTime; // 获取 实时 时间
        GameHelper.getInstance.tickLogTime = tickLogTime + dt; // 更新 实时 时间

        let fireSceneTickFlag: boolean = GameFireScene.tickFlag;
        let isFireSceneSelect: boolean = this.checkCurrentScene(GameFireScene.selfName);

        let startSceneTickFlag: boolean = GameStartScene.tickFlag;
        let isStartSceneSelect: boolean = this.checkCurrentScene(GameStartScene.selfName);

        let pauseSceneTickFlag: boolean = GamePauseScene.tickFlag;
        let isPauseSceneSelect: boolean = this.checkCurrentScene(GamePauseScene.selfName);

        if (fireSceneTickFlag && isFireSceneSelect) {
            this.gameFireScene.onTick(); // 执行 心跳 函数
        }

        if (startSceneTickFlag && isStartSceneSelect) {
            this.gameStartScene.onTick(); // 执行 心跳 函数
        }

        if (pauseSceneTickFlag && isPauseSceneSelect) {
            this.gamePauseScene.onTick();
        }

        // console.log("Status：" + GameHelper.getInstance.keyBoardDelayFlag);
        console.log("Scene：" + this.currentApplyScene);

    }


    /**
     * 校验 当前 应用场景
     * @param sceneName
     */
    public checkCurrentScene(sceneName: string): boolean {

        let getName: string = sceneName;
        let currentSceneName: string = this.currentApplyScene;

        if (getName == currentSceneName) {
            return true;
        }

        return false;
    }


    /**
     * 显示 游戏 战斗 场景
     */
    public showGameFireScene(): void {

        this.gameFireScene = GameFireScene.getInstance;

        if (this.gameFireScene.initCompleteFlag == false) {
            this.gameFireScene.init();
            // this.gameFireScene.debugShapeDraw();
        }

        this.addSceneByName(GameFireScene.selfName);
    }

    /**
     * 显示 游戏 结束 场景
     */
    public showGameEndScene(): void {

        this.gameEndScene = GameEndScene.getInstance;

        if (this.gameEndScene.initCompleteFlag == false) {
            this.gameEndScene.init();
            this.gameEndScene.debugShapeDraw();
        }

        this.addSceneByName(GameEndScene.selfName);
    }

    /**
     * 显示 游戏 暂停 场景
     */
    public showGamePauseScene(): void {

        this.gamePauseScene = GamePauseScene.getInstance;

        if (this.gamePauseScene.initCompleteFlag == false) {
            this.gamePauseScene.init();
            // this.gamePauseScene.debugShapeDraw();
        }

        this.addSceneByName(GamePauseScene.selfName);
    }

    /**
     * 显示 游戏 开始 场景
     */
    public showGameStartScene(): void {

        this.gameStartScene = GameStartScene.getInstance;

        if (this.gameStartScene.initCompleteFlag == false) {
            this.gameStartScene.init();
            this.gameStartScene.debugShapeDraw();
        }

        this.addSceneByName(GameStartScene.selfName);
    }


    /**
     * 进入 游戏 战斗 场景
     */
    private enterGameFireScene(): void {

        this.showGameFireScene();
        this.removeSceneByName(GameStartScene.selfName);
        this.setCurrentScene(GameFireScene.selfName);
    }

    /**
     * 打开 游戏 暂停 场景
     */
    private openGamePauseScene(): void {

        // let isAllow: boolean = this.pauseActionRequest(); // 限制 暂停面板 打开频率

        // if (isAllow == false) {
        //     return;
        // }

        GameFireScene.tickFlag = false; // 暂停 战斗场景 心跳函数
        // this.gameFireScene.setPauseStatus(true);
        this.showGamePauseScene();

        this.setCurrentScene(GamePauseScene.selfName);
    }

    /**
     * 获取 暂停场景 执行函数 请求
     */
    public pauseActionRequest(): boolean {

        let delayTime: number = 500; // 至少 延迟 时间
        let intervalTime: number = this.tickLogTime - this.pauseActionTime; // 当前 时间 间隔
        let isAllow: boolean = intervalTime > delayTime;

        if (isAllow == true) {

            let getCurrentTime: number = this.tickLogTime;
            this.pauseActionTime = getCurrentTime; // 更新 执行 时间

            return true;
        }
        return false;
    }


    /**
     * 返回 游戏 开始 场景
     */
    private returnGameStartScene(): void {

        this.gameFireScene.resetScene(); // 重置 场景

        this.removeSceneByName(GameFireScene.selfName);
        this.removeSceneByName(GamePauseScene.selfName);

        this.showGameStartScene();
        this.setCurrentScene(GameStartScene.selfName);
    }

    /**
     * 返回 游戏 战斗 场景
     */
    private returnGameFireScene(): void {

        this.removeSceneByName(GamePauseScene.selfName);
        GamePauseScene.tickFlag = false;
        GameFireScene.tickFlag = true;

        this.setCurrentScene(GameFireScene.selfName);
    }

    /**
     * 移除 选中 场景
     * @param sceneName
     */
    public removeSceneByName(sceneName: string): void {

        let getName: string = sceneName;

        switch (getName) {
            case GameStartScene.selfName:
                GameStartScene.tickFlag = false;
                this.removeChild(this.gameStartScene);
                break;
            case GameFireScene.selfName:
                GameFireScene.tickFlag = false;
                this.removeChild(this.gameFireScene);
                break;
            case GameEndScene.selfName:
                GameEndScene.tickFlag = false;
                this.removeChild(this.gameEndScene);
                break;
            case GamePauseScene.selfName:
                GamePauseScene.tickFlag = false;
                this.removeChild(this.gamePauseScene);
                break;
            default:
                // console.log("default");
                break;
        }

        // console.log("位置：" + this.x + "---" + this.y);
    }

    /**
     * 添加 选中 场景
     * @param sceneName
     */
    public addSceneByName(sceneName: string): void {

        let getName: string = sceneName;

        switch (getName) {
            case GameStartScene.selfName:
                GameStartScene.tickFlag = true;
                this.addChild(this.gameStartScene);
                break;
            case GameFireScene.selfName:
                GameFireScene.tickFlag = true;
                this.addChild(this.gameFireScene);
                break;
            case GameEndScene.selfName:
                GameEndScene.tickFlag = true;
                this.addChild(this.gameEndScene);
                break;
            case GamePauseScene.selfName:
                GamePauseScene.tickFlag = true;
                this.addChild(this.gamePauseScene);
                break;
            default:
                // console.log("default");
                break;
        }

        // console.log("位置：" + this.x + "---" + this.y);
    }

    /**
     * 设置 当前 应用 场景
     * @param sceneName
     */
    public setCurrentScene(sceneName: string): void {

        let getName: string = sceneName;
        this.currentApplyScene = getName;
    }

}